'use strict'

let models = require('../models');
let errors = require('../../config/errors');

let CheckTime = models.CheckTime;

let CheckTimeDao = models.CheckTimeDao;

function createInstance(checkTimeItem) {
    if ( !checkTimeItem ) {
        checkTimeItem = {
            startHour: 0,
            startMinute: 0,
            endHour: 0,
            endMinute: 0,
            status: 0
        };
    }

    let checkTime = new CheckTime({
        startHour: checkTimeItem.startHour,
        startMinute: checkTimeItem.startMinute,
        endHour: checkTimeItem.endHour,
        endMinute: checkTimeItem.endMinute,
        status: checkTimeItem.status,
        applicableDate:convertString(checkTimeItem.applicableDate)
    });

    CheckTimeDao.create(checkTime);

    return checkTime;
}

function convertString(applicableDates){
    let string = "";
    applicableDates.forEach(date=>{
        string += date.date;
        string += ";";
    });
    return string;

}

module.exports = {
    createInstance
};
